package com.nescafe.nescafepromo.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.nescafe.nescafepromo.entity.ExtraPoint;
import com.nescafe.nescafepromo.entity.Participant;
import com.nescafe.nescafepromo.entity.TotalPoints;

public class ParticipantDetailList {

	Map<String, Participant> participantList = new HashMap<String, Participant>();

	public Map<String, Participant> getList() {
		return participantList;
	}

	public void setList(Map<String, Participant> list) {
		this.participantList = list;
	}

	public void addParticiPant(Participant participant) {
		participantList.put(participant.getEmail(), participant);
	}

	public void addTotalPoints(TotalPoints totalPoints) {
		Participant p = this.participantList.get(totalPoints.getParent().getName());
		if (p != null) {
			p.setTotalPoints(totalPoints);
		}
	}

	public void addExtraPoint(ExtraPoint extraPoint) {
		Participant p = this.participantList.get(extraPoint.getParent().getName());
		if (p != null) {
			p.addExtraPoint(extraPoint);
		}
	}

	public List<Participant> getUnSortedList() {
		List<Participant> list = new ArrayList<Participant>(participantList.values());
		return list;
	}
	
	public List<Participant> getSortedList() {
		List<Participant> list = new ArrayList<Participant>(participantList.values());
		Collections.sort(list, new Comparator<Participant>() {
			public int compare(Participant p1, Participant p2) {
				try {
					if (p1.getTotalPoints().getTotalPonts() > p2.getTotalPoints().getTotalPonts()) {
						return -1;
					} else if (p1.getTotalPoints().getTotalPonts() < p2.getTotalPoints().getTotalPonts()) {
						return 1;
					}
				} catch (Exception e) {

				}
				return 0;
			}
		});
		return list;
	}
}
